package customer.zspt.cn.customer.utils;


import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TvToEnjoy {

    private final Enjoy enjoyData;
    private Context mContext;
    private HashMap<String, Bitmap> mSmileyToRes;
    private Pattern mPattern;
    private static TvToEnjoy enjoy = null;
    public static TvToEnjoy getInstant(Context context){
        if(enjoy == null){
            enjoy = new TvToEnjoy(context);
        }
        return enjoy;
    }
    public TvToEnjoy(Context context) {
        mContext = context;
        enjoyData = new Enjoy(context);
        mSmileyToRes = buildSmileyToRes();
        mPattern = buildPattern();
    }
    public CharSequence solveData(String msg) {
        for(int i=0;i<enjoyData.mEnjoyList.length;i++)
        {
            msg = msg.replace("face"+enjoyData.mEnjoyList[i],enjoyData.mEnjoyList[i]);
        }
        return replace(msg);
    }
    private HashMap<String, Bitmap> buildSmileyToRes() {
        HashMap<String, Bitmap> smileyToRes = new HashMap<String, Bitmap>(enjoyData.mEnjoyList.length);
        for (int i = 0; i < enjoyData.mEnjoyList.length; i++) {
            smileyToRes.put(enjoyData.mEnjoyList[i], enjoyData.mBitmaps.get(i));
        }
        return smileyToRes;
    }
    // 构建正则表达式
    private Pattern buildPattern() {
        StringBuilder patternString = new StringBuilder(enjoyData.mEnjoyList.length * 3);
        patternString.append('(');
        for (String s : enjoyData.mEnjoyList) {
            patternString.append(Pattern.quote(s));
            patternString.append('|');
        }
        patternString.replace(patternString.length() - 1, patternString.length(), ")");
        return Pattern.compile(patternString.toString());
    }
    // 根据文本替换成图片
    public CharSequence replace(CharSequence text){
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        Matcher matcher = mPattern.matcher(text);
        Bitmap  mBitmap = null;
        Bitmap faceBi = null;
        while (matcher.find()) {
             faceBi =  mSmileyToRes.get(matcher.group());
            int width = faceBi.getWidth();
            int height = faceBi.getHeight();
            Matrix matrix = new Matrix();
            matrix.postScale(2.7f, 2.7f);
            mBitmap  = Bitmap.createBitmap(faceBi, 0, 0, width, height, matrix, true);
            BitmapDrawable face
                    = new BitmapDrawable(mBitmap);
            face.setBounds(0, 0, face.getIntrinsicWidth(), face.getIntrinsicHeight());
            builder.setSpan(new ImageSpan(face, ImageSpan.ALIGN_BOTTOM),matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if(mBitmap!=null&&faceBi!=null){
            mBitmap = null;
            faceBi = null;
        }
        return builder;
    }
    public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
        Bitmap image = null;
        AssetManager am = context.getResources().getAssets();
        try {
            InputStream is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
}